package br.project.cdd.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import br.project.cdd.bean.PerfilBean;

public class PerfilDAO extends Conexao {
	
	public PerfilDAO(){
		super();		
	}
	
	public ArrayList<PerfilBean> buscarTodosPerfis()
	{
		ArrayList<PerfilBean> listPerfis = new ArrayList<PerfilBean>();
		PerfilBean bean = null;
		
		try
		{
			String sql = "SELECT ID_PERFIL, DSC_PERFIL FROM PERFIL ORDER BY DSC_PERFIL";
			Statement stmt = conexao.createStatement();
			ResultSet consulta = stmt.executeQuery(sql);
			
			while (consulta.next())
			{
				bean = new PerfilBean();				
				bean.setId_perfil(consulta.getInt(1));
				bean.setDsc_perfil(consulta.getString(2));
				
				listPerfis.add(bean);
			}
		}
		catch(SQLException sqlEx)
		{
			sqlEx.printStackTrace();
		}
		
		return listPerfis;
	}
	
	public int buscarPerfilDoUsuario(int idUser){
		int idPerfil = 0;
		
		try{
			String sql = "SELECT ID_PERFIL FROM USUARIO WHERE ID_USUARIO =" + idUser;
			Statement stmt = conexao.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				idPerfil = rs.getInt(1);
			}
		}
		catch(SQLException sq){
			sq.printStackTrace();
		}
		
		return idPerfil;
	}
}
